package com.gzwanchuang.xhgj.ui.page.mine;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;

import com.gzwanchuang.xhgj.R;
import com.gzwanchuang.xhgj.bridge.data.repository.Apis;
import com.gzwanchuang.xhgj.bridge.request.MineRequestViewModel;
import com.gzwanchuang.xhgj.bridge.state.mine.BindMachineViewModel;
import com.gzwanchuang.xhgj.bridge.state.mine.FeedBackViewModel;
import com.gzwanchuang.xhgj.databinding.FragmentBindMachineBinding;
import com.gzwanchuang.xhgj.databinding.FragmentFeedBackBinding;
import com.gzwanchuang.xhgj.net.NetworkManager;
import com.gzwanchuang.xhgj.net.interfaces.OnRequestCallBack;
import com.gzwanchuang.xhgj.proto.Merchantservice;
import com.gzwanchuang.xhgj.ui.ScanActivity;
import com.gzwanchuang.xhgj.ui.base.BaseFragment;
import com.huawei.hms.hmsscankit.ScanUtil;
import com.huawei.hms.ml.scan.HmsScan;
import com.huawei.hms.ml.scan.HmsScanAnalyzerOptions;

/**
 * 文件名 : HomeRootFragment
 * 创建者 : MangoWang
 * 创建日期 : 2019/12/24 16:20
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */
public class BinMachineFragment extends BaseFragment {
    private FragmentBindMachineBinding mBinding;
    private BindMachineViewModel mViewModel;
    private MineRequestViewModel mRequestViewModel;
    // 1.声明常量
    public  final int DEFAULT_VIEW = 0x22;
    private  final int REQUEST_CODE_SCAN = 0X01;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mViewModel = ViewModelProviders.of(this).get(BindMachineViewModel.class);
        mRequestViewModel = ViewModelProviders.of(this).get(MineRequestViewModel.class);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_bind_machine, container, false);
        mBinding = FragmentBindMachineBinding.bind(view);
        mBinding.setClick(new ClickProxy());
        mBinding.setVm(mViewModel);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mRequestViewModel.getErrorString().observe(this, s -> {
            showShortToast(s);
        });

        mRequestViewModel.getResultString().observe(this, s -> {
            showShortToast(s);
            nav().navigateUp();
        });
        mSharedViewModel.sn.observe(this, s -> {
            mViewModel.sn.set(s);
        });

    }


    public class ClickProxy {
        public void back() {
            nav().navigateUp();
        }

        public void submit() {

            String sn = mViewModel.sn.get();
            if (TextUtils.isEmpty(sn)) {
                showShortToast("请先扫描SN号");
                return;
            }

            Merchantservice.bind.Builder builders = Merchantservice.bind.newBuilder();
            builders.setSn(sn);
            NetworkManager.INSTANCE.post(Apis.bind, builders.build().toByteArray(), new OnRequestCallBack() {
                @Override
                public void onOk(byte[] response) {

                }
                @Override
                public void onError(int errorCode, String errorMessage) {
                    showShortToast(errorMessage);
                    if (errorCode == 200) {
                       nav().navigateUp();
                       mSharedViewModel.isNewSn.setValue(true);
                    }
                }
            });
        }

        public void scan() {
            newViewBtnClick();
        }
    }


    // 3.重写onRequestPermissionsResult方法
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (permissions == null || grantResults == null || grantResults.length < 2 || grantResults[0] != PackageManager.PERMISSION_GRANTED || grantResults[1] != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        if (requestCode == DEFAULT_VIEW) {
            //申请权限之后，调用DefaultView扫码界面
            Intent intent = new Intent(mActivity, ScanActivity.class);
            startActivityForResult(intent,REQUEST_CODE_SCAN);
        }

    }

    // 2.设置Button点击事件
    public void newViewBtnClick() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            this.requestPermissions(
                    new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE},
                    DEFAULT_VIEW);
        }
    }
}
